package com.example.xd.mvp_project.base;


import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.INotificationSideChannel;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.xd.mvp_project.R;

/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseFragment extends Fragment implements BaseView, View.OnClickListener {


    public Activity mActivity;
    private View mView;

    public BaseFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        mView = bindView(inflater, container);
        initView(mView, bundle, savedInstanceState);
        return mView;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListener();
        initData();
    }


    /**
     * 绑定视图
     *
     * @param inflater
     * @param container
     * @return
     */
    public abstract View bindView(LayoutInflater inflater, ViewGroup container);

    /**
     * 初始化控件
     *
     * @param view
     * @param bundle
     * @param savedInstanceState
     */
    protected abstract void initView(View view, Bundle bundle, Bundle savedInstanceState);

    /**
     * 设置监听
     */
    protected abstract void setListener();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    @Override
    public void onClick(View v) {
        widgetClick(v);
    }

    /**
     * View点击
     *
     * @param v
     */
    protected abstract void widgetClick(View v);

    /**
     * 页面跳转
     *
     * @param clz
     */
    public void startActivity(Class<?> clz) {
        startActivity(new Intent(mActivity, clz));
    }

    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(mActivity, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void showErr(String err) {

    }

    @Override
    public void showToast(String msg) {

    }

    @Override
    public void hindLoading() {

    }
}
